#include "comm/my_log.h"

static my_log_t    new_log;

const char *str = "[2022/09/14 17:54:01]   [debug] [my_log_test.c:61]      aaaaaaa\n";
void *write_log(void *args)
{
    int fd = (int)(intptr_t)args;
    size_t str_len = strlen(str);

    while (1) {
        write(fd, str, str_len);
    }

    return NULL;
}

void test_write_log()
{
    int                     i;
    my_syscall_result_t     res;
    int                     fd;

    res = my_syscall_open("/tmp/err.log", O_CREAT|O_APPEND|O_WRONLY, 0644);
    assert(res.eno == 0);

    fd = (int)(res.res);

    for (i = 0; i < 16; i++) {
        my_thread_create(write_log, (void *)(intptr_t)fd, MY_TRUE);
    }
}


void *async_output(void *args)
{
    my_log_t *log = (my_log_t *)args;
    char *str = "aaaaaaa";

    while(1) {
        MY_LOG_DEBUG(log, "%s-%s-%s-%d-%s", str, str, str, 1, str);
    }

    return NULL;
}

void test_my_log_async_output()
{
    int         i;

    bzero(&new_log, sizeof(my_log_t));
    my_log_init(&new_log, LOG_LEVEL_DEBUG, "/tmp/err2.log", LOG_ROLLING_DAY);

    for (i = 0; i < 16; i++) {
        my_thread_create(async_output, &new_log, MY_TRUE);
    }
}

void test_log_flush()
{
    my_log_t new_log;
    bzero(&new_log, sizeof(my_log_t));

    my_log_init(&new_log, LOG_LEVEL_DEBUG, "/tmp/error.log", LOG_ROLLING_DAY);
    MY_LOG_DEBUG(&new_log, "33333333");
    MY_LOG_WARN(&new_log, "33333333");
    MY_LOG_ERROR(&new_log, "33333333");
    sleep(6);
    MY_LOG_DEBUG(&new_log, "44444444");
    MY_LOG_WARN(&new_log, "44444444");
    MY_LOG_ERROR(&new_log, "44444444");
    sleep(6);
    MY_LOG_DEBUG(&new_log, "55555555");
    MY_LOG_WARN(&new_log, "55555555");
    MY_LOG_ERROR(&new_log, "55555555");
    sleep(6);
    MY_LOG_DEBUG(&new_log, "666666666");
    MY_LOG_WARN(&new_log, "666666666");
    MY_LOG_ERROR(&new_log, "666666666");
    my_log_flush(&new_log);
    MY_LOG_DEBUG(&new_log, "777777777");
    MY_LOG_WARN(&new_log, "777777777");
    MY_LOG_ERROR(&new_log, "777777777");
    my_log_flush(&new_log);
    sleep(6);
    MY_LOG_DEBUG(&new_log, "8888888");
    MY_LOG_WARN(&new_log, "8888888");
    MY_LOG_ERROR(&new_log, "8888888");

    my_log_stop(&new_log);
}

void test_log_rolling()
{
    my_log_t new_log;
    bzero(&new_log, sizeof(my_log_t));
    my_log_init(&new_log, LOG_LEVEL_DEBUG, "/tmp/error.log", LOG_ROLLING_MINUTE);

    MY_LOG_DEBUG(&new_log, "66666666");
    MY_LOG_WARN(&new_log, "66666666");
    MY_LOG_ERROR(&new_log, "66666666");
    sleep(65);
    MY_LOG_DEBUG(&new_log, "7777777");
    MY_LOG_WARN(&new_log, "7777777");
    MY_LOG_ERROR(&new_log, "7777777");
    sleep(65);
    MY_LOG_DEBUG(&new_log, "8888888");
    MY_LOG_WARN(&new_log, "8888888");
    MY_LOG_ERROR(&new_log, "8888888");
    my_log_stop(&new_log);
}


int main()
{
    my_mem_init(4);
    MY_DEF_LOG_DEBUG("11111111");
    MY_DEF_LOG_WARN("11111111");
    MY_DEF_LOG_ERROR("11111111");
    my_def_log_stop();
    // MY_DEF_LOG_FATAL("fatal test");

    my_def_log_init(LOG_LEVEL_DEBUG, NULL, LOG_ROLLING_MINUTE);
    MY_DEF_LOG_DEBUG("22222222");
    MY_DEF_LOG_WARN("22222222");
    MY_DEF_LOG_ERROR("22222222");
    my_def_log_stop();

    // test_log_flush();
    // test_log_rolling();

    // 25W/s
    // test_write_log();
    // up to 220W/s in release mode
    // test_my_log_async_output();
    // sleep(20);

    return 0;
}